package com.lxk.controller;

import com.lxk.pojo.Billboard;
import com.lxk.service.BillboardService;
import com.lxk.service.UtilService;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author JohnSoybean
 * @create 2020/4/7 19:13
 * @description
 */
@Controller
public class BillboardController {

    @Autowired
    private BillboardService bs;
    @Autowired
    private UtilService us;

    /**
     * 查找广告
     * @param billboardId
     * @return
     */
    @GetMapping("/billboard/query")
    @ResponseBody
    public Result<Billboard> queryBillboard(Integer billboardId) {
        Billboard billboard = bs.selBillboard(billboardId);
        return Result.success(billboard);
    }

    /**
     * 广告列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/billboard")
    public String toCashout(HttpServletRequest req, Model model,
                            Integer pageIndex, Integer num, Integer totalPages,
                            Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 10;
        }
        if (null == totalPages) {
            totalPages = bs.getTotalPages(num);
        }
        PageModel<Billboard> billboards = bs.selBillboardPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", billboards);
        return "billboard";
    }


    /**
     * 新增广告
     * @param billboard
     * @return
     */
    @ResponseBody
    @PostMapping("/billboard/add")
    public Result<Integer> addNewBillboard(Billboard billboard){
        billboard.setBillboardTime(new Date());
        bs.addBillboard(billboard);
        return Result.success(1);
    }

    /**
     * 编辑广告
     * @param billboard
     * @return
     */
    @ResponseBody
    @PostMapping("/billboard/modify")
    public Result<Integer> modifyBillboard(Billboard billboard, MultipartFile file){
        if(!file.isEmpty()){
            String pic = us.uploadPicture(file);
            billboard.setBillboardPic(pic);
        }
        bs.updBillboard(billboard);
        return Result.success(1);
    }


    /**
     * 投放/撤下广告
     * @param billboardId
     * @return
     */
    @GetMapping("/billboard/expire")
    @ResponseBody
    public Result<Integer> shiftExpireBillboard(Integer billboardId) {
        bs.expireBillboard(billboardId);
        return Result.success(1);
    }


    /**
     * 删除广告
     * @param billboardId
     * @return
     */
    @GetMapping("/billboard/delete")
    @ResponseBody
    public Result<Integer> deleteBillboard(Integer billboardId) {
        bs.deleteBillboard(billboardId);
        return Result.success(1);
    }

    /**
     * 切换置顶广告
     * @param billboardId
     * @return
     */
    @GetMapping("/billboard/top")
    @ResponseBody
    public Result<Integer> shiftTopBillboard(Integer billboardId) {
        bs.topBillboard(billboardId);
        return Result.success(1);
    }


}
